<?php              
define('DEFAULT_TABLE_ROWS', 1);

#=================================================================================================================
#=================================================================================================================
#	Define all possible options in drop-down menu
#=================================================================================================================
#=================================================================================================================

$var_cui_furnizor = 0;
if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['cui_furnizor']))
{
$var_cui_furnizor = $_POST['cui_furnizor'];
}
else  if($_SERVER['REQUEST_METHOD'] == "GET" and isset($_GET['cui_furnizor']))
{
	$var_cui_furnizor = $_GET['cui_furnizor'];
}

$form_for_table = array(
#----------------------------------------------------------------------
#						Tabel										  |
#----------------------------------------------------------------------
	
		  
	  
	array('field_type' 			=> 'text',
		  'field_display_name' 	=> 'Nr. decizie respingere',
		  'post_name' 			=> 'nr_decizie_respingere',
		  'field_size' 			=> 40,
		  'max_length' 			=> 100),
		  
	array('field_type' 			=> 'text',
		  'field_display_name' 	=> 'Data decizie respingere',
		  'post_name' 			=> 'data_decizie_respingere',
		  'field_size' 			=> 40,
		  'max_length' 			=> 100),
		  
		  
		 
	array('field_type' 			=> 'text',
		  'field_display_name' 	=> 'Motiv respingere',
		  'post_name' 			=> 'motiv_respingere',
		  'field_size' 			=> 40,
		  'max_length' 			=> 100)
		  
);				
				
$form_fields_with_speficications = array();

for($index = 1; $index <= DEFAULT_TABLE_ROWS; $index+=1) 
{
	foreach( $form_for_table as $form_column_table )
	{
		array_push( $form_fields_with_speficications,$form_column_table);
	}
}

#=================================================================================================================
#	End of drop-down elemnts
#=================================================================================================================


		
# Generate form content based on provided data
function generate_form_content($array_data)
{
	if( !isset($array_data))
	{
		return "";
	}
		
	$get_form_content = "";
	$bFirst = 1;
									
	foreach($array_data as $form_field)
	{
		$change_row = 1;
		
		if(isset($form_field['same_row']))
			if ($form_field['same_row'] == "yes")
					$change_row = 0;
		
		if ( $bFirst )
		{
			$get_form_content .= "<tr>";
		}
		else
		{
			if( $change_row )
			{
				$get_form_content .= "</tr>";	
				$get_form_content .= "<tr>";
			}	
		}			
		
		$bFirst = 0;			
		
		if( isset($form_field['header']))
		{
			$get_form_content .= "</tr><tr><td>".$form_field['header']."</td></tr><tr>";
		}
			
		if( ! isset( $form_field['only_form_element'] ) )
			$get_form_content .= "<td><b><font color=\"".$form_field['current_color']."\">" . $form_field['field_display_name'] . "</font></b></td>";
		
		if( $form_field['field_type'] == 'text' )
		{
			$get_form_content .= "<td><input type =\"text\" name =\"" . $form_field['post_name'] . "\"";
			$get_form_content .= " value=\"" . $form_field['current_value'] . "\"";
			

			$get_form_content .= " size=\"" . $form_field['field_size'] . "\"";
			$get_form_content .= " maxlength=\"" . $form_field['max_length'] . "\"";
			$get_form_content .= " ></td>";
		}
		else if( $form_field['field_type'] == 'drop_down_list' )
		{
			
			if( isset($form_field['initial_values']) )
				$drop_down_list_options = drop_down_list_generator($form_field['initial_values'],$form_field['current_value']);	
			else
				$drop_down_list_options = drop_down_list_generator(null);	
			$get_form_content .= "<td><select name=\"".$form_field['post_name']."\">";
			$get_form_content .= $drop_down_list_options;
			$get_form_content .= "</select></td>";						
		}
		
		
		if( isset( $form_field['extra_space_after'] ))
		{
			$get_form_content .= "</tr>";	
			$get_form_content .= "<tr>";	
			$get_form_content .="<td></td>";
			$get_form_content .= "</tr>";	
			$get_form_content .= "<tr>";	
		}
	}		
	$get_form_content .="</tr>";
	return $get_form_content;
}				
	
# Function used to generate drop down list menu
function drop_down_list_generator($drop_down_list_options_array, $last_selected_value = null)
{ 
	$drop_down_list_options = "";				   
	
	
	if( null == $drop_down_list_options_array )
			return $drop_down_list_options;	
	
	# Default value used when drop-down list is displayed	
	$drop_down_list_options = $drop_down_list_options."<option value=\"neselectat\">...</option>";
					
	# foreach element in dropdown-list render options
	# if last_selected_value is different than null than select that value as being the default one displayed in interface 
	
	
	foreach($drop_down_list_options_array as $drop_down_list_element_value)
	{
		
		if ( $last_selected_value != null )
		{
			if( $last_selected_value == $drop_down_list_element_value )
			{
				# if one of the options in drop-down list matches the provided value for last_selected_value make it the default one
				$drop_down_list_options = $drop_down_list_options."<option selected=\"selected\" value='$drop_down_list_element_value'>$drop_down_list_element_value</option>";	
			}
			else
			{
				$drop_down_list_options = $drop_down_list_options."<option value='$drop_down_list_element_value'>$drop_down_list_element_value</option>";	
			}
			
		}
		else
		{
			$drop_down_list_options = $drop_down_list_options."<option value='$drop_down_list_element_value'>$drop_down_list_element_value</option>";
		}
	}
	return $drop_down_list_options;
}

foreach($form_fields_with_speficications as $form_key => $form_value)
{
	if( ! isset($form_fields_with_speficications[$form_key]['colors']) )
			$form_fields_with_speficications[$form_key]['colors'] = array('first' => '#333333', 'second' => 'red');

	if( ! isset($form_fields_with_speficications[$form_key]['field_size']) )
			$form_fields_with_speficications[$form_key]['field_size'] = null;
	if( ! isset($form_fields_with_speficications[$form_key]['max_length']) )
			$form_fields_with_speficications[$form_key]['max_length'] = null;
	if( ! isset($form_fields_with_speficications[$form_key]['regex']) )
			$form_fields_with_speficications[$form_key]['colors'] = null;						  
	if( ! isset($form_fields_with_speficications[$form_key]['can_be_null']) )
			$form_fields_with_speficications[$form_key]['can_be_null'] =  false;
	
	if( $form_fields_with_speficications[$form_key]['field_type'] == 'text')
	{
		if( isset($form_fields_with_speficications[$form_key]['initial_values']) )
			$form_fields_with_speficications[$form_key]['current_value'] = $form_fields_with_speficications[$form_key]['initial_values'];
		else
			$form_fields_with_speficications[$form_key]['current_value'] =  '';
	}
	else if( $form_fields_with_speficications[$form_key]['field_type'] == 'drop_down_list')
	{
		$form_fields_with_speficications[$form_key]['current_value'] = "neselectat";
	}
	
	$form_fields_with_speficications[$form_key]['current_color'] = $form_fields_with_speficications[$form_key]['colors']['first'];	
}

$form_content = generate_form_content($form_fields_with_speficications);

$erori = "";

$get_form=<<<GATA
	<form method="post" action = "user_commission_search.php">
		<table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
			$form_content
			<input type="hidden" name="add_program" value="true">
			<input type="hidden" name="cui_furnizor" value="$var_cui_furnizor"> 
			<tr><td></td>
				<td><input type ="submit" value = "Salveaza" ></td>
			</tr>
		</table>
	</form>
GATA;
	

if($_SERVER['REQUEST_METHOD'] == 'GET')
{
	echo $get_form;
}


if($_SERVER['REQUEST_METHOD'] == 'POST')
{
		
	$ok = 1;
		foreach($form_fields_with_speficications as $form_key => $form_value)
		{
		if( isset($_POST[$form_fields_with_speficications[$form_key]['post_name']]))
		{									
			if(empty($_POST[$form_fields_with_speficications[$form_key]['post_name']]) 
			|| ( $_POST[$form_fields_with_speficications[$form_key]['post_name']] == 'neselectat') )
			//if (( $_POST[$form_fields_with_speficications[$form_key]['post_name']] == 'neselectat'))
			{
				# Something went wrong with this form element so an error will be displayed to user
				$ok = 0;
				
				# Change color for element ; default will be red
				$form_fields_with_speficications[$form_key]['current_color'] = "red";
				if( isset($form_fields_with_speficications[$form_key]['colors'] ))
				{
					if( isset($form_fields_with_speficications[$form_key]['colors']['second'] ) )
					{
						$form_fields_with_speficications[$form_key]['current_color'] = 
						$form_fields_with_speficications[$form_key]['colors']['second'];		
					}
				}
				
				# Generate an error if nothing it's provided
				$default_generated_error = "";
				
				if( $form_fields_with_speficications[$form_key]['field_type'] == "text" )
				{
					$default_generated_error = 
					"Nu a fost introdus nimic pentru campul ".$form_fields_with_speficications[$form_key]['post_name']."</br>";
				}
				else if( $form_fields_with_speficications[$form_key]['field_type'] == "drop_down_list" )
				{
					$default_generated_error =
					 "Nu a fost selectat nimic pentru campul ".$form_fields_with_speficications[$form_key]['post_name']."</br>";
				}
				else
				{
					$default_generated_error = "Campul ".$form_fields_with_speficications[$form_key]['post_name']." este gol</br>";
				}
				
				# Add the provided error or the default one
				if( isset($form_fields_with_speficications[$form_key]['errors'] ) )
				{
					if( isset($form_fields_with_speficications[$form_key]['errors']['empty_field_or_no_selection_error'] ))
					{
						$erori = $erori.$form_fields_with_speficications[$form_key]['errors']['empty_field_or_no_selection_error'];					
					}
					else
					{
						$erori = $erori.$default_generated_error;
					}
				}
				else
				{
					$erori = $erori.$default_generated_error;
				}
				
				continue;
			}
			
			# Check regex here
			if ( isset( $form_fields_with_speficications[$form_key]['regex'] ) )
			{
				if ( ! empty( $form_fields_with_speficications[$form_key]['regex'] ) )
				{
					if( preg_match($form_fields_with_speficications[$form_key]['regex'],
					$_POST[$form_fields_with_speficications[$form_key]['post_name']])== 0 )
					{																					
						# Something went wrong with this form element so an error will be displayed to user
						$ok = 0;
						
						# Change color for element ; default will be red
						$form_fields_with_speficications[$form_key]['current_color'] = "red";
						if( isset($form_fields_with_speficications[$form_key]['colors'] ))
						{
							if( isset($form_fields_with_speficications[$form_key]['colors']['second'] ) )
							{
								$form_fields_with_speficications[$form_key]['current_color'] = 
								$form_fields_with_speficications[$form_key]['colors']['second'];		
							}
						}
						
						# Generate an error if nothing it's provided
						$default_generated_error = "";
						
						if( $form_fields_with_speficications[$form_key]['field_type'] == "text" )
						{
							$default_generated_error = "Datele introduse pentru campul ".$form_fields_with_speficications[$form_key]['field_display_name']."sunt invalide</br>";
						}
						else
						{
							$default_generated_error = "Campul ".$form_fields_with_speficications[$form_key]['field_display_name']." este invalid</br>";
						}
						
						# Add the provided error or the default one
						if( isset($form_fields_with_speficications[$form_key]['errors'] ) )
						{
							if( isset($form_fields_with_speficications[$form_key]['errors']['no_regex_pattern_found_error'] ))
							{
								$erori = $erori.$form_fields_with_speficications[$form_key]['errors']['no_regex_pattern_found_error'];					
							}
							else
							{
								$erori = $erori.$default_generated_error;
							}
						}
						else
						{
							$erori = $erori.$default_generated_error;
						}
					
						continue;
					}
				}
			}
			
			# check validation functions
			if ( isset( $form_fields_with_speficications[$form_key]['validation_function'] ) )
			{
				if ( !empty( $form_fields_with_speficications[$form_key]['validation_function'] ) )
				{
					$function_name = $form_fields_with_speficications[$form_key]['validation_function'] ;
					$cif_value = $_POST[$form_fields_with_speficications[$form_key]['post_name']]; 
					
					if($function_name($cif_value) == 0 )
					{																					
						# Something went wrong with this form element so an error will be displayed to user
						$ok = 0;
						
						# Change color for element ; default will be red
						$form_fields_with_speficications[$form_key]['current_color'] = "red";
						if( isset($form_fields_with_speficications[$form_key]['colors'] ))
						{
							if( isset($form_fields_with_speficications[$form_key]['colors']['second'] ) )
							{
								$form_fields_with_speficications[$form_key]['current_color'] = 
								$form_fields_with_speficications[$form_key]['colors']['second'];		
							}
						}
						
						# Generate an error if nothing it's provided
						$default_generated_error = "";
						
						if( $form_fields_with_speficications[$form_key]['field_type'] == "text" )
						{
							$default_generated_error = 
							"Datele introduse pentru campul ".$form_fields_with_speficications[$form_key]['post_name']."
							 sunt invalide</br>";
						}
						else
						{
							$default_generated_error = 
							"Campul ".$form_fields_with_speficications[$form_key]['field_display_name']." este invalid</br>";
						}
						
						# Add the provided error or the default one
						if( isset($form_fields_with_speficications[$form_key]['errors'] ) )
						{
							if( isset($form_fields_with_speficications[$form_key]['errors']['no_regex_pattern_found_error'] ))
							{
								$erori = 
									$erori.$form_fields_with_speficications[$form_key]['errors']['no_regex_pattern_found_error'];					
							}
							else
							{
								$erori = $erori.$default_generated_error;
							}
						}
						else
						{
							$erori = $erori.$default_generated_error;
						}
					
						continue;
					}
				}
			}
			$form_fields_with_speficications[$form_key]['current_value'] = $_POST[$form_fields_with_speficications[$form_key]['post_name']];  											
		}
	}
	
	if($ok != 1)
	{
		$form_content = generate_form_content($form_fields_with_speficications);
		$form_post=<<<GATA
			<form method="post" action = "user_commission_search.php">
				<table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
				$form_content
				<input type="hidden" name="number_of_rows" value="3"> 
				<input type="hidden" name="add_program" value="true">
				<input type="hidden" name="cui_furnizor" value="$var_cui_furnizor"> 
				<tr>
				<td></td><td><input type ="submit" value = "Salveaza" onclick="this.form.target='_self';return true;" name="comanda"></td>
				</tr>
		</table>
		</form>
GATA;
		echo $form_post;
		echo "<font color=\"red\">".$erori."</font>";
	}
	else  
	{
					/*CUI_CF , cod , id_furnizori_programe, serie_autorizatie, nr_autorizatie_program,
             data_autorizatie_program, tip_program, SO_SPP, oferta_ucenicie, stare_program,
             nr_decizie_respingere, data_decizie_respingere, motiv_respingere, nr_duplicat_autorizatie,
             data_duplicat_autorizatie, nr_retragere_autorizatie, data_retragere_autorizatie, motiv_retragere_autorizatie*/
		require_once ('mysql_connect.php');
		/*$u = mysqli_query($dbc,"INSERT INTO `furnizori_programe`
		(`CUI_CF`,`cod`,`id_furnizori_programe`,`serie_autorizatie`,`nr_autorizatie_program`, `data_autorizatie_program`,
		`tip_program`,`SO_SPP`,`oferta_ucenicie`,`stare_program`,`nr_decizie_respingere`,`data_decizie_respingere`,`motiv_respingere`,`nr_duplicat_autorizatie`,		
		`data_duplicat_autorizatie`,`nr_retragere_autorizatie`,`data_retragere_autorizatie`,`motiv_retragere_autorizatie`)  
		VALUES ('$_POST[CUI_CF]','$_POST[cod]','$_POST[id_furnizori_programe]','$_POST[serie_autorizatie]','$_POST[nr_autorizatie_program]',
		'$_POST[data_autorizatie_program]', '$_POST[tip_program]','$_POST[SO_SPP]','$_POST[oferta_ucenicie]', '$_POST[stare_program]','$_POST[nr_decizie_respingere]', 
		'$_POST[data_retragere_autorizatie]', '$_POST[motiv_retragere_autorizatie]')");*/
		
		echo $var_cui_furnizor;
		$u = mysqli_query($dbc,"INSERT INTO `furnizori_programe`
		(`CUI_CF`,`nume_program`,`cod`,`serie_autorizatie`,`nr_autorizatie_program`, `data_autorizatie_program`,
		`tip_program`,`SO_SPP`,`oferta_ucenicie`, `stare_program`)  
		VALUES 				 	    						   ('$var_cui_furnizor','$_POST[program_name]','$_POST[cod]','$_POST[serie_autorizatie]','$_POST[nr_autorizatie_program]',
		'$_POST[data_autorizatie_program]', '$_POST[tip_program]','$_POST[SO_SPP]','$_POST[oferta_ucenicie]', 'autorizat')");
		
		echo "<br />";	
		echo "<center><h2><font color=\"#00CC00\">Program a fost adaugat cu succes ! </font></h2></center>";	
		echo "<br />";	
		echo $get_form;
	}
}
				
?> 